/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nhadat.action.form;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import nhadat.common.CommonConstant;
import nhadat.dao.TindangDao;
import nhadat.entity.TblTinDang;
import nhadat.tindang.DientichkhuonvienType;
import nhadat.tindang.DientichxaydungType;
import nhadat.tindang.GiatienType;
import nhadat.tindang.KhuvucType;
import nhadat.tindang.TindangType;
import nhadat.tindangfull.ChieudaikhuonvienType;
import nhadat.tindangfull.ChieudaixaydungType;
import nhadat.tindangfull.ChieungangkhuonvienType;
import nhadat.tindangfull.ChieungangxaydungType;
import nhadat.tindangfull.DientichkhuonvienfullType;
import nhadat.tindangfull.DientichxaydungfullType;
import nhadat.tindangfull.GiatienfullType;
import nhadat.tindangfull.KhuvucfullType;
import nhadat.tindangfull.TindangfullType;
import nhadat.xml.DatabaseAndXMLFileMappingSyncrolization;
import nhadat.xml.StaxParser;
import nhadat.xml.XMLReportLogic;
import nhadat.xml.XMLmanipulate;
import nhadat.xml.objecttype.report.TindeleteType;
import org.apache.catalina.connector.Request;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author huanht
 */
public class PreUpdateActionForm extends org.apache.struts.action.ActionForm {

    private String msts;
    private String xmlString;

    public String getXmlString() {
        return xmlString;
    }

    public void setXmlString(String xmlString) {
        this.xmlString = xmlString;
    }


    public String getMsts() {
        return msts;
    }

    public void setMsts(String msts) {
        this.msts = msts;
    }

    public void deleteTinDang() {

        XMLmanipulate test = new XMLmanipulate();
        StaxParser parser = new StaxParser();
        String path = getServlet().getServletContext().getRealPath("/");



            TindangfullType[] dsTindang;

            DientichkhuonvienfullType dientichkhuonvien = new DientichkhuonvienfullType();
            ChieungangkhuonvienType chieungangkhuonvien = new ChieungangkhuonvienType();
            ChieudaikhuonvienType chieudaikhuonvien = new ChieudaikhuonvienType();

            DientichxaydungfullType dientichxaydung = new DientichxaydungfullType();
            ChieungangxaydungType chieungangxaydung = new ChieungangxaydungType();
            ChieudaixaydungType chieudaixaydung = new ChieudaixaydungType();

            KhuvucfullType khuvuc = new KhuvucfullType();
            GiatienfullType giatien = new GiatienfullType();



            List<TindangfullType> listTinDangFullType = parser.readXMLtoTindangFullTypebyStAX(path + "xml/TindangFull.xml");
            dsTindang = new TindangfullType[listTinDangFullType.size() + 1];
            for (int i = 0; i < listTinDangFullType.size(); i++) {
                dsTindang[i] = listTinDangFullType.get(i);
            }


            dientichkhuonvien.setDvddientichkhuonvien(" ");
            dientichkhuonvien.setSolieu(0);
            chieungangkhuonvien.setDvdchieungangkhuonvien(" ");
            chieungangkhuonvien.setSolieu(0);
            chieudaikhuonvien.setDvdchieudaikhuonvien(" ");
            chieudaikhuonvien.setSolieu(0);


            dientichxaydung.setDvddientichxaydung(" ");
            dientichxaydung.setSolieu(0);
            chieungangxaydung.setDvdchieungangxaydung(" ");
            chieungangxaydung.setSolieu(0);
            chieudaixaydung.setDvdchieudaixaydung(" ");
            chieudaixaydung.setSolieu(0);

            khuvuc.setQuanhuyen(" ");
            khuvuc.setTinhthanh(" ");

            giatien.setSolieu(0);
            giatien.setDvdgiatien(" ");

            TindangfullType deletedTin = new TindangfullType();

            deletedTin.setMatindang(msts);
            deletedTin.setTenloaitin(" ");
            deletedTin.setTenloainha(" ");
            deletedTin.setTenloaigiayto(" ");
            deletedTin.setThongtinchitiet(" ");
            deletedTin.setCautruc(" ");
            deletedTin.setDientichkhuonvien(dientichkhuonvien);
            deletedTin.setChieungangkhuonvien(chieungangkhuonvien);
            deletedTin.setChieudaikhuonvien(chieudaikhuonvien);
            deletedTin.setDientichxaydung(dientichxaydung);
            deletedTin.setChieungangxaydung(chieungangxaydung);
            deletedTin.setChieudaixaydung(chieudaixaydung);
            deletedTin.setDiachi(" ");
            deletedTin.setKhuvuc(khuvuc);
            deletedTin.setHinhanh(" ");
            deletedTin.setTieudetin(" ");
            deletedTin.setGiatien(giatien);
            deletedTin.setNguoidangtin(" ");
            Calendar currentDate = Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
            String dateNow = formatter.format(currentDate.getTime());
            deletedTin.setNgaydangtin(dateNow);
            deletedTin.setLancuoicapnhat(dateNow);
            deletedTin.setSoluotxem(new BigInteger(0+""));
            deletedTin.setStatus("delete");
            deletedTin.setIsActive("False");

            dsTindang[listTinDangFullType.size()] = deletedTin;

            test.createXMLfromTindangFullType(dsTindang, path + "xml/TindangFull.xml");

            //-----------------------------------------------------------------------------------
            //-----------sửa 1 record trong Tindang.xml thành status = delete---------------------

            TindangType[] dsTinDangType;

            
            List<TindangType> listTinDangType = parser.readXMLtoTindangTypebyStAX(path + "xml/Tindang.xml");
            dsTinDangType = new TindangType[listTinDangType.size()];
            for (int i = 0; i < listTinDangType.size(); i++) {
                dsTinDangType[i] = listTinDangType.get(i);
            }

            for (int i = 0; i < dsTinDangType.length; i++) {
                if (dsTinDangType[i].getMatindang().equals(msts)) {

                    dsTinDangType[i].setStatus("delete");

                }
            }

            test.createXMLfromTindangType(dsTinDangType, path + "xml/Tindang.xml");

            //-----------------------------------------------------------------------------------



    }

    public TblTinDang xemChiTiet() {
        TindangDao tindang = new TindangDao();
        int tmp = Integer.parseInt(getServlet().getServletContext().getAttribute(CommonConstant.MAX_MATINDANG_DATABASE).toString());
        if (Integer.parseInt(msts) <= tmp) {
            System.out.println("Lay tu datasbe");
            TblTinDang[] result = tindang.getAllTindangByID(msts);
            if (result != null) {
                return result[0];
            }
        } else {
            System.out.println("Lay tu xml");
            TindangfullType tindangFull = new TindangfullType();

            List<TindangfullType> dsTindang = new ArrayList<TindangfullType>();
            StaxParser readXML = new StaxParser();
            String path = getServlet().getServletContext().getRealPath("/");
            dsTindang = readXML.readXMLtoTindangFullTypebyStAX(path + "xml/TindangFull.xml");
            for (int i = 0; i < dsTindang.size(); i++) {
                if (dsTindang.get(i).getMatindang().trim().equals(msts)) {
                    tindangFull = dsTindang.get(i);
                }
            }
            String loainhaFullXML = path + "xml/TblLoaiNha.xml";
            String loaigiaytoFullXML = path + "xml/TblLoaiGiayTo.xml";
            String khuvucFullXML = path + "xml/TblKhuVuc.xml";
            String loaitinFullXML = path + "xml/TblLoaiTin.xml";

            DatabaseAndXMLFileMappingSyncrolization syn = new DatabaseAndXMLFileMappingSyncrolization();
            TblTinDang result = syn.mapTblTinDangWithTindangFullType(tindangFull, loainhaFullXML, loaigiaytoFullXML, khuvucFullXML, loaitinFullXML);
            if (result != null) {
                return result;
            }
        }




        return null;
    }

    public void recordForReport(String username){
        TindeleteType tin = new TindeleteType();
        tin.setMatindang(msts);
        tin.setNguoidelete(username);

        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNow = formatter.format(currentDate.getTime());
        tin.setTime(dateNow);

        XMLReportLogic xmlrl = new XMLReportLogic();
        xmlrl.setContextPath(getServlet().getServletContext().getRealPath("/"));
        xmlrl.recordTinDelete(tin, CommonConstant.ACTION_DELETE);
    }

    public PreUpdateActionForm() {
        super();
        // TODO Auto-generated constructor stub
    }
}
